import pygame, os, math

def combine_tiles(t,tw,th,sw,d):
  final_set = pygame.Surface((sw*tw,int(math.ceil((len(t)/sw))+1)*th))
  final_set.fill((255,0,255))
  c = 0
  r = 0
  while(c < len(t)):
    for i in range(0,sw):
      if( c < len(t)):
        final_set.blit(t[c],(i*th,r*th))
      c = c + 1
    r = r + 1
  fname = raw_input("Enter file name (no extension)")
  fname = d + "/" + fname + ".bmp"
  pygame.image.save(final_set,fname)

def get_tiles(dirname):
  tiles = []
  for root, dirs, files in os.walk(dirname):
    for file in files:
      tiles.append(pygame.image.load(os.path.join(root,file)))
  return tiles

def main():
  pygame.init()
  d = raw_input("Enter directory: ")
  print d
  t = get_tiles(d)
  w = raw_input("Enter tile width ")
  h = raw_input("Enter tile height ")
  sw = raw_input("Enter tile set max width (in tiles) ")
  combine_tiles(t,int(w),int(h),int(sw),d)

main()
